package cn.fx.org.service.impl;

import cn.fx.basic.exception.BusinessException;
import cn.fx.basic.service.impl.BaseServiceImpl;
import cn.fx.basic.util.BaiduAuditUtils;
import cn.fx.org.domain.Employee;
import cn.fx.org.domain.Shop;
import cn.fx.org.domain.ShopLog;
import cn.fx.org.dto.ShopDto;
import cn.fx.org.mapper.EmployeeMapper;
import cn.fx.org.mapper.ShopMapper;
import cn.fx.org.service.IShopLogService;
import cn.fx.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;

@Service
//查询的方法比较多，使用查询的事务配置
//只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private IShopLogService logMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Transactional
    @Override
    public void settlement(Shop shop) {
        //实现shop入驻流程 1.非空校验 spring中提供了一个类 处理string的
        if (StringUtils.isEmpty(shop.getName())
                || StringUtils.isEmpty(shop.getAddress())
                || StringUtils.isEmpty(shop.getTel())

                || StringUtils.isEmpty(shop.getAdmin().getUsername())
                || StringUtils.isEmpty(shop.getAdmin().getPhone())
                || StringUtils.isEmpty(shop.getAdmin().getEmail())
                || StringUtils.isEmpty(shop.getAdmin().getPassword())
                || StringUtils.isEmpty(shop.getAdmin().getComfirmPassword())
        ) {
            //提示店铺商家，参数不能为空
            throw new BusinessException("参数不能为空");
        }

        //通过百度AI审核名称是否符合要求
        Boolean textCensor = BaiduAuditUtils.TextCensor(shop.getName());
        if (!textCensor) { //名称问题
            throw new BusinessException("涉嫌不合法内容");
        }

        //调用get方法，获取admin对象
        Employee admin = shop.getAdmin();
        //2.两次密码不一致校验
        if (!admin.getPassword().equals(admin.getComfirmPassword())) {
            throw new BusinessException("两次密码不一致校,请重新输入");
        }

        //3.店铺是否注册过校验
        Shop dbShop = shopMapper.getShopByMessage(shop);
        if (dbShop != null) {
            throw new BusinessException("店铺已经入驻，请直接登陆！忘记密码请找回密码");
        }

        //把shop和employee 1.保存employee，获取自增主键
        employeeMapper.save(admin);

        //2.把自增的主键设置到shop表中admin_id中
        shop.setAdmin_id(admin.getId());

        //3.保存shop数据，返回自增主键
        shopMapper.save(shop);

        //4.返回的自增主键设置给employee
        admin.setShop_id(shop.getId());

        //5.更新employee数据
        employeeMapper.update(admin);
    }


    @Transactional
    @Override
    public void pass(ShopLog log) throws MessagingException {
        //1.修改shop状态 通过shop_id查询shop数据，修改状态
        Shop shop = shopMapper.loadById(log.getShop_id());
        //审核通过 修改状态：state = 2
        shop.setState(2);
        shopMapper.update(shop);

        //2.录入日志 shoplog
        log.setState(2);
        log.setAudit_id(1L);
        logMapper.save(log);

        //3.通知商家 邮件发送
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        //设置发送方
        helper.setFrom("1395194017@qq.com");
        //设置主题
        helper.setSubject("店铺激活邮件");
        //设置内容
        helper.setText("<h1>你的店铺已经注册!!!</h1><a href='http://localhost:8080/shop/active/" + log.getShop_id() + "'>点击该链接激活</a>", true);
        //添加附件
        helper.addAttachment("p55.jpg", new File("F:\\java_学习资料\\班级资料\\03_images\\项目二宠物图片\\p55.jpg"));

        //获取收件人
        Employee employee = employeeMapper.loadById(shop.getAdmin_id());

        //helper.setTo(employee.getEmail()); 获取Email邮箱
        helper.setTo("1395194017@qq.com");
        //发送
        javaMailSender.send(mimeMessage);
    }

    @Transactional
    @Override
    public void reject(ShopLog log) throws MessagingException {

        //1.修改shop状态 通过shop_id查询shop数据，修改状态
        Shop shop = shopMapper.loadById(log.getShop_id());
        //审核通过
        shop.setState(4);
        shopMapper.update(shop);

        //2.录入日志 shoplog
        log.setState(4);
        log.setAudit_id(1L);
        logMapper.save(log);

        //3.通知商家 邮件发送
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("1395194017@qq.com");
        helper.setSubject("驳回邮件");
        helper.setText("<h1>你的店铺入驻失败!!!</h1>", true);
        //添加附件
        helper.addAttachment("p55.jpg", new File("F:\\java_学习资料\\班级资料\\03_images\\项目二宠物图片\\p55.jpg"));

        //获取收件人
        Employee employee = employeeMapper.loadById(shop.getAdmin_id());

        //helper.setTo(employee.getEmail()); 获取Email邮箱
        helper.setTo("1395194017@qq.com");
        //发送
        javaMailSender.send(mimeMessage);

        //4.删除入驻信息
        //删除管理员信息
        //employeeMapper.remove(shop.getAdmin_id());
        //删除店铺信息
        //shopMapper.remove(shop.getId());
    }

    @Override
    public List<ShopDto> getEcharts() {
        return shopMapper.getEcharts();
    }

}
